package switchviz.network.netswitch;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.jdom.JDOMException;

import switchviz.core.SwitchVizException;
import switchviz.core.alias.AliasFacade;
import switchviz.core.mib.MIBFacade;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JXMLComboBox;
import switchviz.gui.component.PaddingPanel;
import switchviz.network.NetworkException;
import switchviz.network.NetworkFacade;

public class SwitchTestPanel extends PaddingPanel implements ActionListener, ItemListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -207173376550092990L;
	private static Logger logger = Logger.getLogger(SwitchTestPanel.class);
	private SwitchDataModel _sdm = null;
	private JLabeldComponent _txttest = null;
	private JLabel _lblswitchname = null;
	private JButton _butttest = null;
	private JLabeldComponent _cmbtestport = null;
	private JButton _buttcancel = null;
	private JXMLComboBox _cmbalias = null;
	private String _actoid = "";
	

	
	public SwitchTestPanel(SwitchDataModel sdm)
	{
		try
		{
			_sdm = sdm;
			initialize();
			_lblswitchname.setText(_sdm.getName() + " @ " + _sdm.getAddress() + ":" + _sdm.getPort());
			fillPortCmb();
			checkCmb();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	private void initialize() throws JDOMException, IOException
	{
		_butttest = new JButton("Query OID");
		_butttest.addActionListener(this);
		_buttcancel = new JButton("Cancel");
		_txttest = new JLabeldComponent("Result: ", JLabeldComponent.TEXTAREA);
		_lblswitchname = new JLabel();
		
		_cmbalias = new JXMLComboBox("Test with Alias: ", JXMLComboBox.ALIASLIST);
		_cmbalias.getComboBox().addItemListener(this);
		_cmbtestport = new JLabeldComponent("Test Port: ", JLabeldComponent.COMBOBOX);
		
        JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
        buttonpanel.add(Box.createHorizontalGlue());
        buttonpanel.add(_butttest);
        buttonpanel.add(_buttcancel);
		
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.add(_lblswitchname);
        this.add(_cmbalias);
        this.add(_cmbtestport);
        this.add(_txttest);
        this.add(buttonpanel);
	}
	
	public void addActionListener(ActionListener l)
	{
		_buttcancel.addActionListener(l);
	}

	public void actionPerformed(ActionEvent evt) 
	{
		if(_cmbalias.getComboBox().getItemCount() == 0 || _actoid.equals("")) return;
		String retval = "processing the query...";
		_txttest.setText(retval);
		try 
		{
			String oid = _actoid;
			if(_cmbtestport.isVisible())
			{
				if(_cmbtestport.getText().equals("")) return;
				oid += "." + Integer.valueOf(_cmbtestport.getText());
			}
			Object retob = NetworkFacade.requestData(_sdm, oid);
			if(retob == null)
			{
				retval = "No answer on OID " + oid;
			}
			else
			{
				retval = retob.toString();
				if(retval == "")
					retval = "A Timeout occured! Check the Switch-Configurations.";
			}
			_txttest.setText(retval);
		} 
		catch (NetworkException e) 
		{
			_txttest.setText("A problem occurd. " + e.getMessage());
		}
	}

	public void itemStateChanged(ItemEvent evt) 
	{
		try 
		{
			checkCmb();
		} 
		catch (SwitchVizException e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	private void checkCmb() throws SwitchVizException
	{
		if(_cmbalias.getComboBox().getSelectedIndex() == -1) return;
		_actoid = AliasFacade.getOid(_cmbalias.getText());
		if(MIBFacade.isPortRequired(_actoid))
		{
			_cmbtestport.setVisible(true);
			
		}
		else
		{
			_cmbtestport.setVisible(false);
		}
	}
	
	private void fillPortCmb()
	{
		for(String port : _sdm.getPortList())
		{
			_cmbtestport.getComboBox().addItem(port);
		}
	}
}
